// Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

 #include "shell/browser/ui/message_box.h"

 #include "base/containers/contains.h"
 #include "base/functional/callback.h"
 #include "base/json/json_writer.h"
 #include "base/no_destructor.h"
 #include "base/strings/string_util.h"
 #include "base/strings/utf_string_conversions.h"
 #include "base/task/thread_pool.h"
 #include "base/values.h"
 #include "ohos/adapter/dialog/dialog_adapter.h"
 #include "shell/browser/native_window.h"
 
 #if defined(USE_OZONE)
 #include "ui/base/ui_base_features.h"
 #endif
 
 namespace electron {
 
 MessageBoxSettings::MessageBoxSettings() = default;
 MessageBoxSettings::MessageBoxSettings(const MessageBoxSettings&) = default;
 MessageBoxSettings::~MessageBoxSettings() = default;
 
 namespace {
 
 std::string MessageBoxTypeEnumConvertToStr(const MessageBoxType& type) {
   switch (type) {
     case MessageBoxType::kInformation:
       return "info";
     case MessageBoxType::kWarning:
       return "warning";
     case MessageBoxType::kError:
       return "error";
     case MessageBoxType::kQuestion:
       return "question";
     default:
       return "none";
   }
 }
 
 std::string SettingsConvertToJsonStr(const MessageBoxSettings& settings) {
   int parent_id = -1;
   electron::NativeWindow* parent_window = settings.parent_window;
   if (parent_window) {
     parent_id = parent_window->GetAcceleratedWidget();
   }
   std::string type = MessageBoxTypeEnumConvertToStr(settings.type);
   std::string title = settings.title;
   std::string message = settings.message;
   std::string detail = settings.detail;
   std::string checkbox_label = settings.checkbox_label;
   bool checkbox_checked = settings.checkbox_checked;
   int cancel_id = settings.cancel_id;
   int default_id = settings.default_id;
   base::Value::List buttons;
   for (auto& button : settings.buttons) {
     buttons.Append(button);
   }
 
   base::Value::Dict settings_dict;
   settings_dict.Set("parent_id", parent_id);
   settings_dict.Set("type", type);
   settings_dict.Set("title", title);
   settings_dict.Set("message", message);
   settings_dict.Set("detail", detail);
   settings_dict.Set("checkbox_label", checkbox_label);
   settings_dict.Set("checkbox_checked", checkbox_checked);
   settings_dict.Set("cancel_id", cancel_id);
   settings_dict.Set("default_id", default_id);
   settings_dict.Set("buttons", std::move(buttons));
 
   std::string settings_json;
   base::JSONWriter::Write(settings_dict, &settings_json);
   return settings_json;
 }
 
 void ConvertImageSkiaToMessageBoxIcon(
     const gfx::ImageSkia& image,
     ohos::adapter::dialog::MessageBoxIcon& messageIcon) {
   const SkBitmap* bitmap = image.bitmap();
   if (!bitmap->readyToDraw()) {
     return;
   }
   size_t pixelSize = bitmap->computeByteSize();
   auto pixelData(std::make_unique<char[]>(pixelSize));
   if (!bitmap->getPixels()) {
     return;
   }
   memcpy(pixelData.get(), bitmap->getPixels(), pixelSize);
   messageIcon.width = bitmap->width();
   messageIcon.height = bitmap->height();
   messageIcon.buff = std::move(pixelData);
 }
 
 ohos::adapter::dialog::MessageBoxResult ShowMessageBoxAdapter(
     const MessageBoxSettings& settings) {
   ohos::adapter::dialog::MessageBoxIcon icon;
   ConvertImageSkiaToMessageBoxIcon(settings.icon, icon);
   auto result =
       ohos::adapter::dialog::DialogAdapter::GetInstance().ShowMessageBox(
           SettingsConvertToJsonStr(settings), &icon);
   return result;
 }
 
 }  // namespace
 
 int ShowMessageBoxSync(const MessageBoxSettings& settings) {
   int button_id = ShowMessageBoxAdapter(settings).button_id;
 
   return button_id;
 }
 
 void ShowMessageBox(const MessageBoxSettings& settings,
                     MessageBoxCallback callback) {
   base::ThreadPool::PostTaskAndReplyWithResult(
       FROM_HERE, {base::TaskPriority::BEST_EFFORT},
       base::BindOnce(&ShowMessageBoxAdapter, settings),
       base::BindOnce(
           [](MessageBoxCallback callback,
              ohos::adapter::dialog::MessageBoxResult result) {
             std::move(callback).Run(result.button_id, result.checkbox_checked);
           },
           std::move(callback)));
 }
 
 void CloseMessageBox(int id) {}
 
 void ShowErrorBox(const std::u16string& title, const std::u16string& content) {
   ohos::adapter::dialog::DialogAdapter::GetInstance().ShowErrorBox(
       base::UTF16ToUTF8(title), base::UTF16ToUTF8(content));
 }
 
 }  // namespace electron
 